package com.ksm.version.service.impl;

import com.ksm.version.domain.SysServerAndPackageVersion;
import com.ksm.version.domain.SysVersionManage;
import com.ksm.version.domain.dto.VersionDto;
import com.ksm.version.domain.vo.ProjectVo;
import com.ksm.version.mapper.ProjectMapper;
import com.ksm.version.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired(required = false)
    private ProjectMapper mapper;


    @Override
    public List<VersionDto> findAll() {
        List<SysVersionManage> all = mapper.findAll();
        List<VersionDto> list = new ArrayList<>();
        for (SysVersionManage versionManage : all) {
            VersionDto dto = new VersionDto();
            if (versionManage.getSysDeployHistory() != null) {
                if (versionManage.getCurrentVersion() != null && versionManage.getCurrentVersion().equals(versionManage.getPackageVersion())) {
                    dto.setUpdate(versionManage.getUpdatedTime() != null && versionManage.getSysDeployHistory().getDeployDate().before(versionManage.getUpdatedTime()));
                } else {
                    dto.setUpdate(true);
                }
                dto.setAppCreateTime(versionManage.getCreatedTime());
                dto.setAppCurrentVersion(versionManage.getCurrentVersion());
                dto.setAppUpdateTime(versionManage.getUpdatedTime());
                dto.setAppDeploymentTime(versionManage.getSysDeployHistory().getDeployDate());
                dto.setAppName(versionManage.getPackageName());
                dto.setAppFileName(versionManage.getFileName());
                dto.setAppOwner(versionManage.getCreateBy());
                dto.setAppVersion(versionManage.getPackageVersion());
            } else {
                dto.setUpdate(false);
                dto.setAppCreateTime(versionManage.getCreatedTime());
                dto.setAppName(versionManage.getPackageName());
                dto.setAppFileName(versionManage.getFileName());
                dto.setAppOwner(versionManage.getCreateBy());
                dto.setAppCurrentVersion(versionManage.getCurrentVersion());
            }
            dto.setAppRemoteDevelopment(versionManage.getRemoteDevelopment() == 1);
            dto.setDeployId(versionManage.getDeployId());
            list.add(dto);
        }
        return list;
    }

    @Override
    public SysVersionManage findOne(SysVersionManage sysVersionManage) {

        return mapper.findOne(sysVersionManage.getDeployId());
    }

    @Override
    public Integer updateProject(SysVersionManage sysVersionManage) {
        return mapper.updateProject(sysVersionManage);
    }

    @Override
    public Integer addNewProject(ProjectVo projectVo) {
        SysVersionManage sysVersionManage = new SysVersionManage();
        sysVersionManage.setPackageVersion(projectVo.getProjectVersion());
        sysVersionManage.setCurrentVersion(projectVo.getProjectVersion());
        sysVersionManage.setPackageName(projectVo.getProjectName());
        sysVersionManage.setCreateBy(projectVo.getProjectOwner());
        sysVersionManage.setUrl("D:" + File.separator + "testLocalStorage");

        mapper.addNewProject(sysVersionManage);

        SysServerAndPackageVersion version = new SysServerAndPackageVersion();
        version.setServerId(Integer.valueOf(projectVo.getProjectServerNo()));
        version.setDeployId(sysVersionManage.getDeployId());
        return mapper.addNewServerMid(version);
    }

    @Override
    public Integer updateHistory(Integer id) {
        return mapper.updateHistory(id);
    }

}
